package com.happe.foodeasy.system.service.impl;

import com.happe.foodeasy.dao.LinkedImageDao;
import com.happe.foodeasy.dto.LinkedImageDTO;
import com.happe.foodeasy.system.entity.ImageTargetPosition;
import com.happe.foodeasy.system.entity.LinkedImage;
import com.happe.foodeasy.system.service.ImageTargetPositionService;
import com.happe.foodeasy.system.service.LinkedImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class LinkedImageServiceImpl implements LinkedImageService {
    @Autowired
    private LinkedImageDao linkedImageDao;
    @Autowired
    private ImageTargetPositionService imageTargetPositionService;

    @Override
    public List<LinkedImageDTO> queryLinkedImagesByPositionName(String target) {
        ImageTargetPosition targetPosition = imageTargetPositionService.retrieveImageTargetPositionByName("imageTargetPositionName", target);
        List<LinkedImage> linkedImages = linkedImageDao.findManyByProperty("imageTargetPositionId", targetPosition.getId());
        return LinkedImageDTO.sourceToDTO(linkedImages);
    }

    @Override
    public byte[] searchImageById(long imageId) {
        LinkedImage image = linkedImageDao.findOneByProperties("id", imageId);
        return image.getImageByte();
    }
}
